package net.bolue.sysmng.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.bolue.sysmng.db.DbOpUtil;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.sysmng.tblbean.Dic;
import net.bolue.sysmng.tblbean.DicGrp;
import net.bolue.sysmng.tblbean.RefDicRes;
import net.bolue.web.entry.FormDataOption;
import net.bolue.web.report.RepConstants;

public class DictUtil {

	/**
	 * 获得数字字典描述
	 * @param subSys
	 * @param grpId
	 * @param value
	 * @return
	 */
	public static String getDicDesc(String subSys, int grpId, int value)throws BolueException{
		String sql = "SELECT * FROM st_dic WHERE SUBSYS='"+subSys+"' and DICGRPID="+grpId+" and DICID="+value;
		DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY);
		Dic dic = dbOpUtil.getOne(Dic.class, sql, null);;
		return dic.getDicDesc();
	}
	
	/**
	 * 获得数字字典
	 * @param subSys
	 * @param grpId
	 * @return
	 */
	public static Map<Integer,String> getDicMap(String subSys, int grpId)throws BolueException{
		List<Dic> dicList = getDicList(subSys,grpId);
		Map<Integer,String> dicMap = new HashMap<Integer,String>();
		if(dicList!=null&&dicList.size()>0){
			for(Dic dic : dicList){
				dicMap.put(dic.getDicId(), dic.getDicDesc());
			}
		}
		return dicMap;
	}
	
	public static List<Dic> getDicList(String subSys, int grpId)throws BolueException{
		String sql = "SELECT * FROM st_dic WHERE SUBSYS='"+subSys+"' and DICGRPID="+grpId;
		DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY);
		List<Dic> dicList = dbOpUtil.query(Dic.class, sql, null);
		return dicList;
	}
	
	/**
	 * 获得字典下拉选项
	 * @param subSys
	 * @param grpId
	 * @return
	 * @throws BolueException
	 */
	public static List<FormDataOption> getOptionOfDic(String subSys, int grpId)throws BolueException{
		DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY);
		String dicGrpSql = "SELECT * FROM st_dicgrp WHERE SUBSYS='"+subSys+"' and DICGRPID="+grpId;
		DicGrp dicGrp = dbOpUtil.getOne(DicGrp.class, dicGrpSql, null);
		
		String sql = "SELECT * FROM st_dic WHERE SUBSYS='"+subSys+"' and DICGRPID="+grpId;
		List<FormDataOption> optionList = null;
		List<Dic> dicList = dbOpUtil.query(Dic.class, sql, null);
		if(dicList!=null&&dicList.size()>0){
			optionList = new ArrayList<FormDataOption>();
			for(Dic dic : dicList){
				optionList.add(dicToOption(dic,dicGrp));
			}
		}
		return optionList;
	}
	
	public static RefDicRes getRefDic(String subSys, int dicSubKind)throws BolueException{
		String sql = "SELECT * FROM st_refdicres WHERE SUBSYS='"+subSys+"' and DICSUBKIND="+dicSubKind;
		DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY);
		RefDicRes refDicRes = dbOpUtil.getOne(RefDicRes.class, sql, null);
		return refDicRes;
	}
	
	public static List<Map<String,Object>> getDataByRefDic(RefDicRes refDicRes) throws BolueException{
		String table = refDicRes.getTblName();
		String[] columns = {refDicRes.getDicIdCol(),refDicRes.getDicDescCol()};
		DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY);
//		String sql = "SELECT "++","+refDicRes.getDicDescCol()+" FROM "+table;
		return dbOpUtil.query( table, columns, null, null);
	}
	
	public static List<FormDataOption> getOptionOfRefDic(String subSys, int dicSubKind) throws BolueException{
		String sql = "SELECT * FROM st_refdicres WHERE SUBSYS='"+subSys+"' and DICSUBKIND="+dicSubKind;
		DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY);
		RefDicRes refDicRes = dbOpUtil.getOne(RefDicRes.class, sql, null);
		if(refDicRes==null){
			throw new BolueException(1, "没有找到"+subSys+"."+dicSubKind+"的引用字典信息!");
		}
		List<FormDataOption> optionList = new ArrayList<FormDataOption>();
		List<Map<String,Object>> dataList = getDataByRefDic(refDicRes);
		if(dataList!=null&&dataList.size()>0){
			for(Map<String,Object> d : dataList){
				FormDataOption formDataOption = new FormDataOption();
				formDataOption.setKey(d.get(refDicRes.getDicIdCol())+"");
				formDataOption.setValue(d.get(refDicRes.getDicDescCol())+"");
				optionList.add(formDataOption);
			}
		}
		return optionList;
	}
	
	public static List<Dic> getAllKindDicList(int dicKind,String subSys, int grpId)throws BolueException{
		if (dicKind == 0){//无字典
			return null;
		}
		if(subSys==null||"".equals(subSys)){
			return null;
		}
		if(grpId==0){
			return null;
		}
		if(dicKind == 1){//数字字典
			return getDicList(subSys,grpId);
		}else if(dicKind == 4){
			
		}
		return null;
	}
	
	public static FormDataOption dicToOption(Dic dic,DicGrp dicGrp){
		if(dic==null){
			return null;
		}
		FormDataOption formDataOption = new FormDataOption();
		formDataOption.setKey(dic.getDicId()+"");
		String value = "";
		if(dicGrp.getDdDescType()==1){
			value = dic.getDicId()+":"+dic.getDicDesc();
		}else if(dicGrp.getDdDescType()==2){
			value = dic.getDicId()+"";
		}else if(dicGrp.getDdDescType()==3){
			value = dic.getDicDesc();
		}
		formDataOption.setValue(value);
		return formDataOption;
	}
	
	public static List<FormDataOption> getOptionOfAllDic(int dicKind,String subSys, int grpId) throws BolueException{
		if (dicKind == 0){//无字典
			return null;
		}
		if(subSys==null||"".equals(subSys)){
			return null;
		}
		if(grpId==0){
			return null;
		}
		if(dicKind == 1){//数字字典
			return getOptionOfDic(subSys,grpId);
		}else if(dicKind == 4){
			return getOptionOfRefDic(subSys,grpId);
		}
		return null;
	}
	
	public static Map<String,List<FormDataOption>> getDicMaps(String[] subSysArray, Integer[] dicKindArray, Integer[] dicGrpIdArray) throws BolueException{
		if (dicKindArray.length != dicGrpIdArray.length){
			throw new BolueException(5,"参数dicKinds与dicGrpIDs的大小不同!");
		}
		Map<String,List<FormDataOption>> dicsMap = new HashMap<String,List<FormDataOption>>();
		for (int i = 0; i < dicKindArray.length; i++){
			if(dicKindArray[i]==null||dicKindArray[i]==0){
				continue;
			}
			if(dicsMap.get(dicKindArray[i]+"_"+dicGrpIdArray[i])!=null){
				continue;
			}
			if (dicGrpIdArray[i] == null){
				throw new BolueException(5,"数字字典的字典组标识不能为null!");
			}
			List<FormDataOption> optionList = getOptionOfAllDic(dicKindArray[i], subSysArray[i], dicGrpIdArray[i]);
			if(optionList!=null&&optionList.size()>0){
				dicsMap.put(dicKindArray[i]+"_"+dicGrpIdArray[i], optionList);
			}
		}
		return dicsMap;
	}
}
